package com.gdssoft.pdaweb.dao.water.impl;
/*
 *  捷达世软件（深圳）有限公司 地铁三号线考勤系统
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gdssoft.core.Constants;
import com.gdssoft.core.dao.impl.BaseDaoImpl;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.pdaweb.dao.water.PdaWaterMeterDao;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.model.water.WaterMeterPlan;

@SuppressWarnings("unchecked")
public class PdaWaterMeterDaoImpl extends BaseDaoImpl<PdaWaterMeter> implements PdaWaterMeterDao{

	public PdaWaterMeterDaoImpl() {
		super(PdaWaterMeter.class);
	}
	
	@Override
	public List<PdaWaterMeter> getAllForExcel() {
		return findByHql("from PdaWaterMeter p where p.type=2");
	}
	
	@Override
	public List<PdaWaterMeter> getWaterMeterExcel(String idS) {
		List<PdaWaterMeter> list =new ArrayList<PdaWaterMeter>();
		try{
			list = findByHql("from PdaWaterMeter P WHERE P.type=2 AND P.department.depId in ("+idS+") ");
		}catch(Exception e){
//			e.printStackTrace();
			logger.debug("PdaWaterMeter query error:"+e.getMessage());			
		}
		return list;
	}
	
	@Override
	public PdaWaterMeter getByMeterId(String meterId) {
		List<PdaWaterMeter> list = new ArrayList<PdaWaterMeter>();
		try{
			list = findByHql("from PdaWaterMeter pm where pm.amId='"+meterId+"'");
		}catch(Exception e){
			logger.debug("PdaWaterMeter query error:"+e.getMessage());			
		}
		return list.size()>0?list.get(0):new PdaWaterMeter();
	}

	@Override
	public List<PdaWaterMeter> getByPosition(String districtId,String buildingId) {
		List<PdaWaterMeter> list = new ArrayList<PdaWaterMeter>();
		try{
			String hql = "from PdaWaterMeter pm where pm.isPlanned=? and pm.districtId=?";
			
			hql += buildingId.isEmpty()?"":" and pm.buildingId='"+buildingId+"'";
			
			list = findByHql(hql,new Object[]{Constants.UNPLANNED,districtId});
		}catch(Exception e){
			logger.debug("PdaWaterMeter query error:"+e.getMessage());			
		}
		return list;
	}
	@Override
	public List<PdaWaterMeter> getByHouseId(String houseId){
		String hql = "from PdaWaterMeter pm where pm.aparmentId=?";
		return this.findByHql(hql,new String[]{houseId});
	}
	@Override
	public PdaWaterMeter getByBarcode(String barCode){
		String hql = "from PdaWaterMeter pm where pm.pdaMdAssetInfo.barCodebarCode=?";
		return (PdaWaterMeter) findUnique(hql, new String[]{barCode});
		
	}

	@Override
	public boolean isCrmMeterNoExist(String crmMeterNo) {
		//return findByHql("from PdaWaterMeter p where p.crmMeterNo=?",new Object[]{crmMeterNo}).size()>0;
		return false;
	}

	@Override
	public List<PdaWaterMeter> getSelectWaterMeter(String districtId, String[] ids) {
		// TODO Auto-generated method stub
		List<PdaWaterMeter> list = new ArrayList<PdaWaterMeter>();
		for(String id : ids){
			String hql = "from PdaWaterMeter where districtId = '" + districtId + "' and amId = '" + id +"'";
			if(this.findByHql(hql).size()>0){
				PdaWaterMeter pdaWaterMeter = this.findByHql(hql).get(0);
				list.add(pdaWaterMeter);
			}
			
		}
		
		return list;
	}

	@Override
	public boolean pauseAndResume(String recId, String vType, AppUser appUser,
			String dateT) {
		// TODO Auto-generated method stub
		String sql ="UPDATE PDA_WATER_METER" 
		           +"    SET TYPE = ?, LASTEDITBY = ?, LASTEDITDT = ?"  
				   +"  WHERE METER_ID = ?";
		String[] param= new String[]{vType,appUser.getUsername(),dateT,recId};
		int count = jdbcTemplate.update(sql,param);
		if(count>0){
			return true;
		}
		return false;
	}
}